home *** CD-ROM | disk | FTP | other *** search
/ IRIS Performer 2.2 Friends Demo / SGI IRIS Performer 2.2 Friends Demo.iso / friends / devices / BGSystems / bg / init_lv.c < prev    next >
C/C++ Source or Header  |  1997-10-31  |  4KB  |  197 lines

  1. /*
  2.  * Copyright 1992-4   BG Systems, Inc.
  3.  * init_lv.c
  4.  *
  5.  * Routine to init a Rev 3.0 Box
  6.  *
  7.  * Author         Date       Comments
  8.  * John Green     15-Oct-94  Major revision for v 3.0 EPROM
  9.  * John Green     21-Oct-94  Broken out of fblib.c
  10.  * John Green     14-Jan-95  Added acknowledge
  11.  * John Green     01-Feb-95  Final touches for rev 3.0 release
  12.  * John Green     21-Feb-95  Fixed bug when init_lv() called again
  13.  * John Green     15-Mar-95  Fixed dig out setup bug
  14.  * John Green     03-Apr-95  Allow 2.4 EPROM
  15.  * John Green     17-Apr-95  return() rather than exit() if setup fails
  16.  */
  17.  
  18. static char SccsId[] = "@(#)init_lv.c    1.12 19 Apr 1995";
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. #include "lv3.h"
  24.  
  25. extern RS_ERR rs_err;
  26.  
  27. extern int w_lv(int, char *);
  28. extern int get_ack(int );
  29. extern int set_baud(int, int );
  30. extern int check_rev(bglv *);
  31. extern int check_setup(bglv *);
  32.  
  33. int init_lv(bglv *bgp)
  34. {
  35.    char c1, c2, c3;
  36.    char str[5];
  37.    int  st;
  38.    int  i;
  39.  
  40.    st = check_setup(bgp);
  41.    if ( st < 0 )
  42.       return(st);
  43.  
  44. /*
  45.  *  Compute the number of channels requested, and the 
  46.  *  appropriate string length.
  47.  */
  48.  
  49. /*
  50.  *  Analog inputs
  51.  */
  52.    bgp->n_analog_in = 0;
  53.    for ( i=0; i < 8; i++)
  54.       if ( (bgp->analog_in >> i) & 0x1 )
  55.          bgp->n_analog_in++;
  56.  
  57. /*
  58.  *  Digital inputs
  59.  */
  60.  
  61.    switch(bgp->dig_in)
  62.    {
  63.     case 0x0:
  64.       bgp->n_dig_in = 0;
  65.       break;
  66.     case 0x10:
  67.     case 0x20:
  68.     case 0x40:
  69.       bgp->n_dig_in = 8;
  70.       break;
  71.     case 0x30:
  72.     case 0x50:
  73.     case 0x60:
  74.       bgp->n_dig_in = 16;
  75.       break;
  76.     case 0x70:
  77.       bgp->n_dig_in = 24;
  78.       break;
  79.    }
  80.  
  81. /*
  82.  *  Digital outputs
  83.  */
  84.  
  85.    switch(bgp->dig_out)
  86.    {
  87.     case 0x0:
  88.       bgp->n_dig_out = 0;
  89.       break;
  90.     case 0x10:
  91.     case 0x20:
  92.     case 0x40:
  93.       bgp->n_dig_out = 8;
  94.       break;
  95.     case 0x30:
  96.     case 0x50:
  97.     case 0x60:
  98.       bgp->n_dig_out = 16;
  99.       break;
  100.     case 0x70:
  101.       bgp->n_dig_out = 24;
  102.       break;
  103.    }
  104.  
  105.  
  106. /*
  107.  *  Analog outputs
  108.  */
  109.    bgp->n_analog_out = 0;
  110.    if ( bgp->analog_out > 0 )
  111.    {
  112.       for ( i=0; i < 3; i++)
  113.          if ( (bgp->analog_out >> i) & 0x1 )
  114.             bgp->n_analog_out++;
  115.    }
  116.  
  117.  
  118. /*
  119.  *  Set the string length for receiving data
  120.  */
  121.    bgp->str_len  = 2 + (2*bgp->n_analog_in) + (bgp->n_dig_in/4);
  122.  
  123.  
  124. /*
  125.  *  First character has the baud rate and the lower 4 analog ins.
  126.  */
  127.    c1 =  bgp->baud;
  128.    c1 |= (bgp->analog_in & 0xf);
  129. /*
  130.  *  Second character has the digital inputs and the upper 4 analog ins
  131.  */
  132.    c2 =  bgp->dig_in;
  133.    c2 |= (bgp->analog_in & 0xf0) >> 4;
  134.  
  135.    if ( bgp->Rev.major == 3 )
  136.    {
  137.       str[0] = 's';
  138. /*
  139.  *  Third character (for rev 3 eproms only, has the digital outs (-F)
  140.  *  and analog outs (-3G)
  141.  */
  142.       c3 =  bgp->analog_out & 0xf;
  143.       c3 |= bgp->dig_out & 0xf0;
  144. /*
  145.  *  Add the OFFSET to each character to make sure they are not control
  146.  *  characters
  147.  */
  148.       str[1] = c1 + OFFSET;
  149.       str[2] = c2 + OFFSET;
  150.       str[3] = c3 + OFFSET;
  151.       str[4] = '\0';
  152.       st = w_lv(bgp->sp_fd, str);
  153. /*
  154.  *  Make sure that the LV got the setup !
  155.  */
  156.       st = get_ack(bgp->sp_fd);
  157. /*
  158.  *  If we have a rev 3.00 eprom, just don't check the return
  159.  *  value - just proceed and assume things are OK.
  160.  *  (Bug fixed in 3.01)
  161.  */
  162.       if ( bgp->Rev.bug != 0 )
  163.       {
  164.          if ( st < 0 )
  165.             return(st);
  166.       }
  167.    }
  168.    else if ( bgp->Rev.major == 2 )
  169.    {
  170.       if ( bgp->Rev.minor == 2 )
  171.       {
  172. /*
  173.  *  For rev 2.2 EPROMS use an 'R' and no offset -- so make sure c1 and
  174.  *  c2 are not flow control characters !
  175.  */
  176.          str[0] = 'R';
  177.          str[1] = c1;
  178.          str[2] = c2;
  179.       }
  180.       else if ( bgp->Rev.minor >= 3 )
  181.       {
  182. /*
  183.  *  For rev 2.3 EPROMS use an 'r' and offset the characters
  184.  */
  185.          str[0] = 'r';
  186.          str[1] = c1 + OFFSET;
  187.          str[2] = c2 + OFFSET;
  188.       }
  189.       str[3] = '\0';
  190.       st = w_lv(bgp->sp_fd, str);
  191.    }
  192.  
  193.    st = set_baud(bgp->sp_fd, bgp->baud);
  194.  
  195.    return(0);
  196. }
  197.